﻿using System;
using System.Linq;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TfsCommitMonitor.Tests.Mocking;
using TfsCommitMonitor.Tfs;
using Changeset = TfsCommitMonitor.Tfs.Changeset;

namespace TfsCommitMonitor.Tests
{
	[TestClass]
	public class TfsBusinessTest
	{
		[TestInitialize]
		public void TestInitialize()
		{
			Configuration.Instance.MaxChangesToPersist = 10;
			Configuration.Instance.MaxNumberOfChangesetsOnInitialLoad = 5;
		}
		
		public static readonly Uri TestCollectionUri = new Uri("https://tfs.codeplex.com/tfs/tfs34");
		public static readonly String TestProject = "$/TfsCommitMonitor";
		
		[TestMethod]
		public void TestPollForChanges_InitialLoad()
		{
			TfsBusiness target = new TfsBusiness(new TestCredentialProvider());
			Changeset[] results = target.PollForChanges(TestCollectionUri, TestProject, null);
			Int32 expected = Configuration.Instance.MaxNumberOfChangesetsOnInitialLoad;
			Int32 actual = results.Count();
			Assert.AreEqual(expected, actual);
		}

		[TestMethod]
		public void TestPollForChanges_Latest()
		{
			TfsBusiness target = new TfsBusiness(new TestCredentialProvider());
			Changeset[] results = target.PollForChanges(TestCollectionUri, TestProject, null);

			Int32 lastChangeSetId = results.First().ChangesetId;

			results = target.PollForChanges(TestCollectionUri, TestProject, lastChangeSetId);

			const int expected = 0;
			Int32 actual = results.Count();

			Assert.AreEqual(expected, actual);
		}

		[TestMethod]
		public void TestPollForChanges_LatestExceptOne()
		{
			TfsBusiness target = new TfsBusiness(new TestCredentialProvider());
			Changeset[] results = target.PollForChanges(TestCollectionUri, TestProject, null);

			Int32 lastChangeSetId = results.Skip(1).First().ChangesetId;

			results = target.PollForChanges(TestCollectionUri, TestProject, lastChangeSetId);
			
			const int expected = 1;
			Int32 actual = results.Count();
				
			Assert.AreEqual(expected, actual);
		}

		[TestMethod]
		[Ignore]	//No branches in CodePlex
		public void TestPollForChanges_LatestExceptOneAcrossBranches()
		{
			TfsBusiness target = new TfsBusiness(new TestCredentialProvider());

			String mainProject = TestProject + "/Main";
			Changeset[] results = target.PollForChanges(TestCollectionUri, mainProject, null);

			Int32 lastChangeSetId = results.Skip(1).First().ChangesetId;
			results = target.PollForChanges(TestCollectionUri, mainProject, lastChangeSetId);

			Int32 expected = 1;
			Int32 actual = results.Count();

			Assert.AreEqual(expected, actual, "Main");

			String branchProject = TestProject + "/TestBranch";
			results = target.PollForChanges(TestCollectionUri, branchProject, null);

			lastChangeSetId = results.Skip(1).First().ChangesetId;
			results = target.PollForChanges(TestCollectionUri, branchProject, lastChangeSetId);

			expected = 1;
			actual = results.Count();

			Assert.AreEqual(expected, actual, "Branch");
		}		

		[TestMethod]
		public void GetChangesForChangesetTest()
		{
			TfsBusiness target = new TfsBusiness(new TestCredentialProvider());
			
			var changeset = target.PollForChanges(TestCollectionUri, TestProject, null).First();

			var changes = changeset.GetTfsChanges();

			var editChange = 
				changes
				.Where(change => change.ChangeType == ChangeType.Edit)
				.FirstOrDefault();

			if (editChange != null)
				target.DownloadChanges(TestCollectionUri, TestProject, editChange);
			else
				Assert.Fail("Last Checkin did not have an edit");
		}
	}
}
